package com.gmlive.common.sample.diagnose

import android.graphics.Color
import android.os.Bundle
import android.util.TypedValue
import android.view.Gravity
import android.view.ViewGroup
import android.widget.Button
import android.widget.FrameLayout
import android.widget.Toast
import com.gmlive.common.ui.app.IkCompatActivity
import com.gmlive.common.ui.app.annotation.Immerse
import com.meelive.ingkee.iknetwork.diagnose.impl.IKNetworkDiagnoseService
import com.meelive.ingkee.iknetwork.diagnose.model.BackGroundConfig
import com.meelive.ingkee.iknetwork.diagnose.model.CopyViewConfig
import com.meelive.ingkee.iknetwork.diagnose.model.IKNetworkDiagnoseBaseInfo
import com.meelive.ingkee.iknetwork.diagnose.model.TitleViewConfig

@Immerse(translucentStatus = true, darkStatusBar = true, fitSystemWindows = true)
class NetworkDiagnoseActivity: IkCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val parent = FrameLayout(this)
        parent.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)

        val btn = Button(this)
        btn.text = "点击开始诊断"
        btn.setOnClickListener {
            gotoNetworkDiagnose()
        }
        btn.layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT).apply {
            gravity = Gravity.CENTER
        }
        parent.addView(btn)

        setContentView(parent)
    }

    fun gotoNetworkDiagnose() {
        // 必需
        val baseInfo = IKNetworkDiagnoseBaseInfo(
                10086,
                "imei",
                "isoCountryCode",
                "carrname",
                "mobilCountryCode",
                "mobilNetCode")

        // 必需
        val httpHost = "https://service.inke.cn"
        val httpPath = "/api/appconfig/get_raw"
        val httpParam = "network_diagnose_config_android_ss"

        // 非必需, 有默认样式
        val copyViewConfig = CopyViewConfig.Builder()
                .setBackgroundStrokeColor(Color.GREEN) // 背景边框颜色
                .setBackgroundStrokeWidth(1) // 背景边框粗细
                .setTextPressColor(Color.RED) // 文字按压时的颜色
                .setTextUnPressColor(Color.GREEN) // 文字未按压时的颜色
                .setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18f) // 文字大小
                .setUnPressBackgroundConfig(BackGroundConfig(Color.TRANSPARENT, 45f)) // 按压时背景色
                .setPressBackgroundConfig(BackGroundConfig(Color.GREEN, 45f)) // 未按压时背景色
                .build()

        // 非必需, 有默认样式
        val titleViewConfig = TitleViewConfig(
                Color.RED,  // 返回键颜色
                Color.RED) // title 文字颜色

        IKNetworkDiagnoseService.Builder()
                .setBaseInfo(baseInfo) // 设置基本信息：uid、imei、mobilContryCode 等，必须项
                .setAppConfigUrl(httpHost, httpPath) // 设置请求 AppConfig 的链接，必需项
                .setAppConfigParam(httpParam) // 设置 AppConfig链接的请求参数，必需项
                //.setCopyCheckViewConfig(copyViewConfig) // 设置 "复制诊断报告" 的ui样式，有默认样式，非必需项
                //.setTitleViewConfig(titleViewConfig) // 设置 头部ui颜色，有默认样式，非必需项
                .build()
                .startNetworkDiagnose(context = this) { errorMsg: String? ->  // 打开 CheckNetworkActivity 的错误回调
                    Toast.makeText(this, errorMsg, Toast.LENGTH_LONG).show()
                }
    }
}